const { spawn } = require("child_process");

// 执行
function execNode(cmd, param) {
    let workerProcess;
    let promise = new Promise((resolve) => {
        let argArr = Array.isArray(param) ? param : [];
        workerProcess = spawn(cmd, argArr, {
            stdio: ["inherit", "inherit", "inherit"],
            encoding: "utf-8",
        });
        workerProcess.on("exit", (code, signal) => {
            console.log("子进程 exit", code, signal);
            resolve({ code });
        });
        workerProcess.on("error", (err) => {
            console.log("进程出错了-----error", err);
            resolve({ code: -9898 });
        });
    });
    const kill = () => {
        workerProcess && workerProcess.kill();
    };
    return {
        promise,
        kill,
    };
}
module.exports = {
    execNode,
};
